package com.fstech.whiteboadrd.control;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fstech.whiteboadrd.R;

public class ControlManager implements View.OnClickListener {

    private View mRootView;
    private LinearLayout controlPanel1;
    private ImageView controlMenu;
    private ImageView controlPen;
    private ImageView controlErase;
    private ImageView controlClear;
    private ImageView controlWithdraw;
    private ImageView controlGo;
    private LinearLayout controlPanel2;
    private ImageView controlBg;
    private ImageView controlAdd;
    private ImageView controlPrevious;
    private TextView controlIndex;
    private TextView controlTotal;
    private ImageView controlNext;

    private ControlCallback mCallback;
    private LinearLayout control_pages;

    public ControlManager(View rootView) {
        mRootView = rootView;
        initView();
        initEvent();
    }

    public void showPages(int index, int total) {
        controlIndex.setText(index + "");
        controlTotal.setText("/" + total);
    }

    public void setCallback(ControlCallback callback) {
        mCallback = callback;
    }

    private void initEvent() {
        if (controlMenu == null) {
            return;
        }
        controlMenu.setOnClickListener(this);
        controlPen.setOnClickListener(this);
        controlErase.setOnClickListener(this);
        controlClear.setOnClickListener(this);
        controlWithdraw.setOnClickListener(this);
        controlGo.setOnClickListener(this);
        controlBg.setOnClickListener(this);
        controlAdd.setOnClickListener(this);
        controlNext.setOnClickListener(this);
        controlPrevious.setOnClickListener(this);
        control_pages.setOnClickListener(this);
    }

    private void initView() {
        controlPanel1 = (LinearLayout) mRootView.findViewById(R.id.control_panel1);
        controlMenu = (ImageView) mRootView.findViewById(R.id.control_menu);
        controlPen = (ImageView) mRootView.findViewById(R.id.control_pen);
        controlErase = (ImageView) mRootView.findViewById(R.id.control_erase);
        controlClear = (ImageView) mRootView.findViewById(R.id.control_clear);
        controlWithdraw = (ImageView) mRootView.findViewById(R.id.control_withdraw);
        controlGo = (ImageView) mRootView.findViewById(R.id.control_go);
        controlPanel2 = (LinearLayout) mRootView.findViewById(R.id.control_panel2);
        control_pages = (LinearLayout) mRootView.findViewById(R.id.control_pages);
        controlBg = (ImageView) mRootView.findViewById(R.id.control_bg);
        controlAdd = (ImageView) mRootView.findViewById(R.id.control_add);
        controlPrevious = (ImageView) mRootView.findViewById(R.id.control_previous);
        controlIndex = (TextView) mRootView.findViewById(R.id.control_index);
        controlTotal = (TextView) mRootView.findViewById(R.id.control_total);
        controlNext = (ImageView) mRootView.findViewById(R.id.control_next);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.control_menu:
                if (mCallback != null) {
                    mCallback.onMenu(false, controlMenu);
                }
                break;
            case R.id.control_pen:
                if (mCallback != null) {
                    mCallback.onPen(false, controlPen);
                }
                break;
            case R.id.control_erase:
                if (mCallback != null) {
                    mCallback.onErase(false, controlErase);
                }
                break;
            case R.id.control_clear:
                if (mCallback != null) {
                    mCallback.onClear(false, controlClear);
                }
                break;
            case R.id.control_withdraw:
                if (mCallback != null) {
                    mCallback.onWithdraw(false);
                }
                break;
            case R.id.control_go:
                if (mCallback != null) {
                    mCallback.onGo(false);
                }
                break;
            case R.id.control_bg:
                if (mCallback != null) {
                    mCallback.onBg(false, controlBg);
                }
                break;
            case R.id.control_add:
                if (mCallback != null) {
                    mCallback.onAdd(false);
                }
                break;
            case R.id.control_previous:
                if (mCallback != null) {
                    mCallback.onPrevious();
                }
                break;
            case R.id.control_next:
                if (mCallback != null) {
                    mCallback.onNext();
                }
                break;
            case R.id.control_pages:
                if (mCallback != null) {
                    mCallback.onPages(false, controlAdd);
                }
                break;
            default:
                break;
        }
    }

    public void swichEditType(int type) {
        switch (type) {
            // pen
            case 0:
                controlErase.setImageResource(R.mipmap.ic_erase);
                controlPen.setImageResource(R.mipmap.ic_pen);
                break;
            // erase
            case 1:
                controlErase.setImageResource(R.mipmap.erase_active);
                controlPen.setImageResource(R.mipmap.pen_inactive);
                break;
        }
    }

    public interface ControlCallback {

        void onMenu(boolean isPop, View controlClear);

        void onErase(boolean isPop, View controlClear);

        void onClear(boolean isPop, View controlClear);

        void onBg(boolean isPop, View controlClear);

        void onPen(boolean isPop, View controlClear);

        void onWithdraw(boolean isPop);

        void onGo(boolean isPop);

        void onAdd(boolean isPop);

        void onPrevious();

        void onNext();

        void onPages(boolean isPop, View anchor);

        void onInsert();

        void onOpen();

        void onquit();

        void onSave(String name);

        void onPageIndexRefresh();

        void onPageDel(int position);

        void onPageSel(int position);

        void onShare(int i);

        void onInsertChart();

    }

}
